//
//  ShopViewController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/21.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "ShopViewController.h"
#import "ShopModel.h"
#import "ShopCell.h"
#import "PWNetWorking.h"
#import "UIImageView+WebCache.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefreshHeader.h"
#import <MJRefresh/MJRefresh.h>



@interface ShopViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger start;

@end

@implementation ShopViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.start = 0;
    self.dataArr = [NSMutableArray array];
    [self dataHandler];
    [self creareTableView];
    
    
}
- (void)creareTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[ShopCell class] forCellReuseIdentifier:@"cell"];
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.dataArr = [NSMutableArray array];
        self.start = 0;
        [self dataHandler];
        //停止刷新
        [self.tableView.mj_header endRefreshing];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //加载数据
        self.start += 10;
        [self dataHandler];
        //停止加载
        [self.tableView.mj_footer endRefreshing];
    }];

    [self.view addSubview:_tableView];
    [_tableView release];
}
- (void)dataHandler
{
    NSString *url = @"http://api2.pianke.me/pub/shop";
    // auth=&client=1&deviceid=DB3A1424-3E92-41D9-8C86-10F84629D968&limit=10&start=0&version=3.0.6
    NSDictionary *bodyDic =@{@"auth": @"", @"client": @"1", @"deviceid":@"DB3A1424-3E92-41D9-8C86-10F84629D968", @"limit":@"10",@"start":@"0", @"version":@"3.0.6"};
    [PWNetWorking POSTWithURL:url body:bodyDic completion:^(id result) {
        NSArray *arr = result[@"data"][@"list"];
        NSArray *addArr = [ShopModel modelHanderWithArray:arr];
        for (ShopModel *m in addArr) {
            [self.dataArr addObject:m];
        }
        NSLog(@"%@", result);
        [_tableView reloadData];
    }];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (self.dataArr.count > 0) {
        ShopModel *m = _dataArr[indexPath.row];
        [cell.coverImg sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
        cell.titleLabel.text = m.title;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
